﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/

//Module : Base64Coder.CPP
//Purpose: Definition for the Base64 encoder / decoder class
//Created: PJN / 20-06-2000
//History: None
//
//Copyright (c) 1998 - 2002 by PJ Naughter.  
//All rights reserved.

/////////////////////////////// Defines ///////////////////////////////////////
#ifndef __BASE64CODER_H__
#define __BASE64CODER_H__

/////////////////////////////// Classes ///////////////////////////////////////

namespace xos_common
{
    class base64  
    {
    public:
        //Constructors / Destructors
        base64();
        ~base64();

    public:
        //方法
        void    Encode(const char *, unsigned long);
        void    Decode(const char *, unsigned long);
        const char *  DecodedMessage() const;
        const char *  EncodedMessage() const;
        long    DecodedMessageSize() const;
        long    EncodedMessageSize() const;

    protected:
        // Internal bucket class.
        class TempBucket
        {
        public:
            unsigned char         nData[4];
            unsigned char         nSize;
            void        Clear() { memset( nData, 0, 4); nSize = 0; };
        };

        //变量
        char *    m_pDBuffer;
        char *    m_pEBuffer;
        unsigned long    m_nDBufLen;
        unsigned long    m_nEBufLen;
        unsigned long    m_nDDataLen;
        unsigned long    m_nEDataLen;

        static char m_DecodeTable[256];
        static bool m_Init;

        //M方法
        void     AllocEncode(unsigned long);
        void     AllocDecode(unsigned long);
        void     SetEncodeBuffer(const char * pBuffer, unsigned long nBufLen);
        void     SetDecodeBuffer(const char * pBuffer, unsigned long nBufLen);
        void    _EncodeToBuffer(const TempBucket &Decode, char * pBuffer);
        unsigned long    _DecodeToBuffer(const TempBucket &Decode, char * pBuffer);
        void    _EncodeRaw(TempBucket &, const TempBucket &);
        void    _DecodeRaw(TempBucket &, const TempBucket &);
        bool    _IsBadMimeChar(unsigned char );
        void            _Init();
    };
} // xos_common

#endif //__BASE64CODER_H__
